#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdkhooks>
#include <sdktools>


ConVar C_WitchDamage;

float F_WitchDamage;

#define CVAR_FLAGS			FCVAR_NOTIFY

#define PLUGIN_NAME                   "l4d2_chainsaw_damage_tank_witch"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "修改电锯对坦克&&女巫的伤害."   //介绍
#define PLUGIN_VERSION                "1.0"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站

public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}

public void OnPluginStart()
{


	C_WitchDamage = CreateConVar("G_l4d2_chainsaw_damage_tank_witch", "1.0", "设置电锯对坦克&&女巫的伤害. -1.0=禁用,0.0=免疫电锯伤害,0.01=1%,类推,1.0=100%(1刀死),大于1.0=实际伤害.", CVAR_FLAGS);
	
	CreateConVar("l4d2_chainsaw_damage_tank_witch_version", PLUGIN_VERSION, "插件版本", FCVAR_NOTIFY);	
	
	C_WitchDamage.AddChangeHook(convar_changed);

	//AutoExecConfig(true, "l4d2_chainsaw_damage_tank_witch");
}

void convar_changed(ConVar convar, const char[] oldValue, const char[] newValue)
{
	get_cvars();
}

void get_cvars()
{
	F_WitchDamage = C_WitchDamage.FloatValue;
}

public void OnConfigsExecuted()
{
	get_cvars();
}

public void OnClientPutInServer(int client)
{
	SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
}

public void OnEntityCreated (int entity, const char[] classname)
{
	if(entity <= MaxClients || !IsValidEntity(entity))
		return;
		
	if(classname[0] != 'w')
		return;
		
	if(strcmp(classname, "witch") == 0)
		SDKHook(entity, SDKHook_OnTakeDamage, WitchOnTakeDamage);
}


public Action OnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &weapon, float damageForce[3], float damagePosition[3], int damagecustom)
{
	if( inflictor > MaxClients && inflictor < 2048 && IsValidClient(attacker))
	{

		
		static char classname[32];
		GetEdictClassname(inflictor, classname, sizeof(classname));
		
		if(strcmp(classname, "weapon_chainsaw") == 0 )
		{
				
			if(IsTank(victim))
			{
				//damage = F_WitchDamage;
				damage = F_WitchDamage > 1.0 ? F_WitchDamage : F_WitchDamage * GetEntProp(victim, Prop_Data, "m_iMaxHealth");					
				return Plugin_Changed;
			}	
		
		}
	}
	return Plugin_Continue;
}

public Action WitchOnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &weapon, float damageForce[3], float damagePosition[3])
{
	if( inflictor > MaxClients && inflictor < 2048 && IsValidClient(attacker))
	{

		
		static char classname[32];
		GetEdictClassname(inflictor, classname, sizeof(classname));
		
		if(strcmp(classname, "weapon_chainsaw") == 0 )
		{
				
			if(IsWitch(victim))
			{
				//damage = F_WitchDamage;
				damage = F_WitchDamage > 1.0 ? F_WitchDamage : F_WitchDamage * GetEntProp(victim, Prop_Data, "m_iMaxHealth");				
				return Plugin_Changed;
			}	
		
		}
	}
	return Plugin_Continue;
}

//玩家团队判定
bool IsValidClient(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
}

//女巫判定
bool IsWitch(int victim)
{
	if(victim > MaxClients)
	{
		static char classname[6];
		GetEdictClassname(victim, classname, sizeof(classname));
		return strcmp(classname, "witch") == 0;
	}
	return false;
}


//坦克判定
bool IsTank(int victim)
{
	return victim > 0 && victim <= MaxClients && IsClientInGame(victim) && GetClientTeam(victim) == 3 && GetEntProp(victim, Prop_Send, "m_zombieClass") == 8;
}
